/**
 * Created by nickzhao on 15/10/27.
 */
    window.DAL = {
      database : 'abatracker',
        desc: 'ABA Tracker Databse',
        databaseSize : 8*1024*1024,
        version : '1.0',
        message : function (status,message) {
            return {
                success : status,
                response : message
            }
        },
        getConnection : function () {
            var me = this;
            var connection = openDatabase(me.database, me.version, me.desc, me.databaseSize);
            return connection;
        },
        closeDB : function (IDBDatabase) {

        },
        queryBySQL : function (sql,callbackFunc) {
            var me = this;
            var connection = me.getConnection();
            connection.transaction(function (context){
               context.executeSql(sql,[], function (transaction, result) {
                   var dataList = [];
                   for (var i = 0; i < result.rows.length; i++){
                       dataList.push(result.rows.item(i))
                   }
                   if(callbackFunc){
                       callbackFunc(me.message(true,dataList));
                   }
               }, function (transaction, error) {
                   if(callbackFunc){
                       callbackFunc(me.message(false,error.message));
                   }
               });
            });
        },
        queryForPage : function (sql,start,limit,callbackFunc) {
            var me = this;
            var connection = me.getConnection();
            if(!start || !limit){
                callbackFunc(me.message(false,'no param'));
                return ;
            }
            me.queryBySQL('select * from ( ' + sql + ") limit " + start + ',' + limit,callbackFunc)
        },
        updateBySQL : function (sql,callbackFunc) {
            var me = this;
            var connection = me.getConnection();
            connection.transaction(function (context){
                context.executeSql(sql,[], function (transaction, result) {
                    if(callbackFunc){
                        callbackFunc(me.message(true,result.rowsAffected));
                    }
                }, function (transaction, error) {
                    if(callbackFunc){
                        callbackFunc(me.message(false,error.message));
                    }
                });
            });
        },
        /**
         * 批量更新update语句，只要有一条语句执行失败，事务自动回滚
         * @param sqlArray
         * @param callbackFunc
         */
        updateByArray : function (sqlArray,callbackFunc) {
            var me = this;
            var connection = me.getConnection();
            if(sqlArray){
                    connection.transaction(function (context){
                        for(var i = 0; i < sqlArray.length; i++){
                             context.executeSql(sqlArray[i],[], function (transaction, result) {}, function (transaction, error) {
                                 callbackFunc(me.message(false,error.message));
                             });
                        }
                    })
            }
        }
    };

    $("#btn").click(function () {
        var arrays = [];
        arrays.push('INSERT INTO testTable (id, name) VALUES (0, "Byron")');
        arrays.push('INSERT INTO testTable (id, name) VALUES (13, "Byron")');
        DAL.updateByArray(arrays, function (res) {
            console.log(res);
        })
    });